#个人中心模块
from . import user_blue
from flask import render_template,g,redirect,url_for,request,abort,jsonify,current_app,session
from info.utils.comment import user_login_data
from info import response_code,db,constants
from info.utils.file_storge import upload_file
from info.models import Category,News,User


@user_blue.route('/other_news_list')
def other_news_list():
    # 1.获取页数
    page = request.args.get("p", '1')
    other_id = request.args.get("user_id")

    # 2.校验参数
    try:
        p = int(page)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数错误')

    if not all([page, other_id]):
        return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')

    # 3.查询用户数据
    try:
        user = User.query.get(other_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询用户数据失败')
    if not user:
        return jsonify(errno=response_code.RET.NODATA, errmsg='用户不存在')

    # 4.分页查询
    try:
        paginate = News.query.filter(News.user_id == user.id).paginate(p, constants.OTHER_NEWS_PAGE_MAX_COUNT, False)
        # 获取当前页数据
        news_list = paginate.items
        # 获取当前页
        current_page = paginate.page
        # 获取总页数
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=response_code.RET.DBERR, errmsg='查询用户数据失败')

    # 5.构造响应数据
    news_dict_list = []
    for news_item in news_list:
        news_dict_list.append(news_item.to_review_dict())

    data = {
        "news_list": news_dict_list,
        "total_page": total_page,
        "current_page": current_page
    }

    # 6.渲染界面
    return jsonify(errno=response_code.RET.OK, errmsg='OK',data=data)




@user_blue.route('/other_info')
@user_login_data
def other_info():
    '''其他用户概况'''
    #1.获取用户登陆信息
    login_user = g.user
    if not login_user:
        return redirect(url_for('index.index'))

    #获取被登陆用户关注的用户的信息
    other_id = request.args.get('user_id')
    if not other_id:
        abort(404)

    #查询要展示的被关注的用户的信息
    other = None
    try:
        other = User.query.get(other_id)
    except Exception as e:
        current_app.logger.error(e)
        abort(404)
    if not other:
        abort(404)

    #判断关注和取消关注
    is_followed = False
    if login_user and other:
        if other in login_user.followed:
            is_followed = True

    context = {
        'user':login_user.to_dict(),
        'other':other.to_dict(),
        'is_followed':is_followed
    }

    return render_template('news/other.html',context = context)





@user_blue.route('/user_follow')
@user_login_data
def user_followed():
    '''我的关注'''
    # 1.获取登录用户的信息
    login_user = g.user
    if not login_user:
        return redirect(url_for('index.index'))


    # 2.接受参数
    page = request.args.get('p', '1')

    # 3.校验参数
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = '1'

    #4.查询登陆用户关注的用户（login_user.followed）
    followed_user_list = []
    total_page = 1
    current_page = 1
    try:
        paginate = login_user.followed.paginate(page,constants.USER_FOLLOWED_MAX_COUNT,False)
        followed_user_list = paginate.items
        total_page = paginate.pages
        current_page = paginate.page
    except Exception as e:
        current_app.logger.error(e)
        abort(404)
    #5.构造渲染数据
    followed_dict_list = []
    for followed_user in followed_user_list:
        followed_dict_list.append(followed_user.to_dict())
    context = {
        'users':followed_dict_list,
        'total_page':total_page,
        'current_page':current_page
    }






    return render_template('news/user_follow.html',context = context)







@user_blue.route('/news_list')
@user_login_data
def user_news_list():
    '''我发布的新闻列表'''
    #1.获取登录用户的信息
    user = g.user
    if not user:
        return redirect(url_for('index.index'))
    #2.接受参数
    page = request.args.get('p','1')

    #3.校验参数
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = '1'

    #4.分页查询
    news_list = []
    current_page = 1
    total_page = 1
    try:
        paginate = News.query.filter(News.user_id==user.id).paginate(page,constants.USER_COLLECTION_MAX_NEWS,False)
        # 5.构造渲染数据
        news_list = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)

    news_dict_list = []
    for news in news_list:
        news_dict_list.append(news.to_basic_dict())

    context = {
        'news_list':news_dict_list,
        'current_page':current_page,
        'total_page':total_page
    }

    #6.渲染界面
    return render_template('news/user_news_list.html',context = context)









@user_blue.route('/news_release',methods = ['post','get'])
@user_login_data
def news_release():
    '''新闻发布'''
    #1.获取用户的登录信息
    user = g.user
    if not user:
        return render_template(url_for('index.index'))

    #2.get请求逻辑:渲染发布新闻的界面
    if request.method == 'GET':
        #2.1渲染新闻数据
        categories = []
        try:
            categories = Category.query.all()
        except Exception as e:
            current_app.logger.error(e)
        #最新分类不是用户需要绑定的
        categories.pop(0)
        context = {
            'categories':categories
        }

        return render_template('news/user_news_release.html',context = context)

    #3.post请求，实现新发布的任务
    if request.method == 'POST':
        #3.1接受参数
        title = request.form.get('title')
        source = '个人发布'
        digest = request.form.get('digest')
        content = request.form.get('content')
        index_image = request.files.get('index_image')
        category_id = request.form.get('category_id')


        #3.2 校验参数
        if not all([title,source,digest,content,index_image,category_id]):
             return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')


        #3.3读取用户上传的新闻图片
        try:
            index_image_data = index_image.read()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.PARAMERR, errmsg='读取新闻图片失败')



        #3.4 将用户上传的新闻转存到七牛
        try:
            key = upload_file(index_image_data)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.THIRDERR, errmsg='上传新闻图片失败')

        #3.5创建news新闻的模型对象，并附值同步到数据库
        news = News()
        news.title = title
        news.digest = digest
        news.source = source
        news.content = content
        #纯粹是迫不得已，因为我们的是新闻类的网站，里面大部分都是爬虫爬的，爬虫会将新闻图片的全路径url趴下来，
        #那么我们就需要存储新闻图片的全路径url
        news.index_image_url = constants.QINIU_DOMIN_PREFIX + key
        news.category_id = category_id
        news.user_id = g.user.id
        #1.代表审核状态
        news.status = 1

        try:
            db.session.add(news)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.DBERR, errmsg='保存新闻数据失败')


        #3.6响应新闻发布结果
        return jsonify(errno=response_code.RET.OK, errmsg='发布成功')












@user_blue.route('/user_collection')
@user_login_data
def user_collection():
    '''用户收藏'''
    #1.获取登录用户信息
    user = g.user
    if not user:
        return redirect(url_for('index.index'))

    #2.接受参数
    page = request.args.get('p','1')

    #3.校验参数
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = '1'

    #4.分页查询：user.collection_news == BaseQuery类型的对象
    paginate = None
    print(page,type(page))

    try:
        paginate = user.collection_news.paginate(page,constants.USER_COLLECTION_MAX_NEWS,False)
    except Exception as e:
        current_app.logger.error(e)

    #5.构造渲染模板的数据
    news_list = paginate.items
    total_page = paginate.pages
    current_page = paginate.page

    news_dict_list = []
    for news in news_list:
        news_dict_list.append(news.to_basic_dict())

    context = {
        'news_list':news_dict_list,
        'total_page':total_page,
        'current_page':current_page
    }

    #6.渲染模板
    return render_template('news/user_collection.html',context = context)












@user_blue.route('/pass_info',methods = ['GET','POST'])
@user_login_data
def pass_info():
    '''修改密码'''
    #1.获取登录用户的信息
    user = g.user
    if not user:
        return redirect(url_for('index.index'))

    #2.get请求逻辑：渲染修改密码的界面
    if request.method == 'GET':
        return render_template('news/user_pass_info.html')

    #3.post请求逻辑，渲染修改密码的界面
    if request.method == 'POST':
        #3.1 接受参数
        old_password = request.json.get('old_password')
        new_password = request.json.get('new_password')

        #3.2校验参数
        if not all([old_password,new_password]):
             return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')

        #3.3判断输入的旧的密码是否时该登录用户的密码
        if not user.check_password(old_password):
             return jsonify(errno=response_code.RET.PARAMERR, errmsg='原密码输入有误')

        #3.4更新密码：#password：setter方法
        user.password = new_password
        #3.5同步到数据库
        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.DBERR, errmsg='保存修改后的密码失败')
        #3.5响应修改密码的结果
        return jsonify(errno=response_code.RET.OK, errmsg='OK')












@user_blue.route('/pic_info',methods=['GET','POST'])
@user_login_data
def pic_info():
    '''用户头像上传'''
    #1.获取参数（图片）
    user = g.user
    if not user:
        return redirect(url_for('index.index'))
    #2.渲染用户头像界面
    if request.method == 'GET':
        context = {
            'user':user.to_dict()
        }
        return render_template('news/user_pic_info.html',context = context)
    #3.上传用户头像
    if request.method == 'POST':
        #3.1获取上传的图片
        avatar_file = request.files.get('avatar')
        try:
            avatar_data = avatar_file.read()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.PARAMERR, errmsg='获取头像失败')

        #3.2上传头像
        try:
            key = upload_file(avatar_data)
        except Exception as e:
            # db.session.rollback()
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.THIRDERR, errmsg='七牛上传头像失败')
        #3.3 保存图片上传后的唯一标识
        user.avatar_url = key
        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.DBERR, errmsg='保存用户头像失败')

        #3.4 响应上传结果
        data ={'avatar_url':constants.QINIU_DOMIN_PREFIX + key}
        return jsonify(errno=response_code.RET.OK, errmsg='头像上传成功',data = data)






@user_blue.route('/base_info',methods = ['POST','GET'])
@user_login_data
def base_info():
    '''基本资料'''
    user = g.user
    if not user:
        return redirect(url_for('index.index'))
    #2.实现get请求逻辑

    if request.method == 'GET':
        '''构造选渲染数据的上下文'''

        context = {
            'user': user.to_dict()

        }

        return render_template('news/user_base_info.html',context = context)
    #3.post 请求逻辑
    if request.method == 'POST':
        #3.1 获取参数（签名，昵称，性别）
        nick_name = request.json.get('nick_name')
        signature = request.json.get('signature')
        gender = request.json.get('gender')

        #3.2校验参数
        if not all([nick_name,signature,gender]):
             return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数')
        if gender not in ['MAN','WOMAN']:
             return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数错误')

        #3.3修改用户基本信息
        user.nick_name = nick_name
        user.signature = signature
        user.gender = gender
        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(e)
            return jsonify(errno=response_code.RET.DBERR, errmsg='修改用户资料失败')


        #3.4‘注意’修改昵称以后，将状态保持信息中的昵称页修改
        session['nick_name'] = nick_name


        #3.5响应修改的结果
        return jsonify(errno=response_code.RET.OK, errmsg='OK')





@user_blue.route('/info')
@user_login_data
def user_info():
    '''个人中心入口
    提示：必须是登陆用户才可以进入
    '''
    #1.获取用户的登陆信息
    user = g.user
    if not user:
        return redirect(url_for('index.index'))
    context = {
        'user':user.to_dict()
        #user继承的base，所以user里面的信息base里面也有

    }
    return render_template('news/user.html',context = context)